//
//  JHTitleView.m
//  
//
//  Created by 谈Xx on 15/12/4.
//  Copyright © 2015年 谈Xx. All rights reserved.
//

#import "JHTitleView.h"

@implementation JHTitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setTitleFrame];
    }
    return self;
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setTitleFrame];
    }
    return self;
}

// 初始化子控件布局
-(void)setTitleFrame
{
    self.bounds = CGRectMake(0, 0, ScreenW, NaviBarHeight);
    self.backgroundColor = [UIColor blackColor];
    self.title = @"";
    [self addTitleSubview];
 

}
// 增加子控件
-(void)addTitleSubview
{
    
    // 添加返回按钮
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
//    back.imageEdgeInsets = UIEdgeInsetsMake(10, 0, 10, 30);
//    UIImage *backImg = [UIImage imageNamed:@"navigationButtonReturnClick"];
//    [back setImage:backImg forState:UIControlStateNormal];
    [back setTitleColor:PQ_COLOR_YELLOW forState:UIControlStateNormal];
//    [back setTitle:@"返回" forState:UIControlStateNormal];
    back.titleLabel.font = FONT(RATIO(36));
    [back addTarget:self action:@selector(returnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:back];
    self.btnBack = back;
    
    // 添加标题
    UILabel *title = [[UILabel alloc] init];
    title.text = self.title;
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = PQ_COLOR_YELLOW;
    title.font = FONT(RATIO(36));
    [self addSubview:title];
    self.titleLabel = title;
   
    // 右边按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.imageEdgeInsets = UIEdgeInsetsMake(10, 5, 10, 5);
    [self addSubview:btn];
//    btn.tag = JHTitleTypeRight;
    self.rightBtn = btn;

}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    //添加约束
    [self.btnBack makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(RATIO(5));
        make.centerY.equalTo(self.titleLabel);
        make.size.equalTo(CGSizeMake(54, 44));
    }];
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.btnBack.right).offset(10);
        make.top.equalTo(self).offset(20);
        make.bottom.equalTo(self);
        make.centerX.equalTo(self);
    }];
    [self.rightBtn makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self);

        make.centerY.equalTo(self.titleLabel);
        make.size.equalTo(CGSizeMake(54, 44));
    }];
    if (self.isHidebtnBack)
    {
        self.btnBack.hidden = YES;
    }

    // 如果没有设置type 默认值 remove
    if (self.titleType == JHTitleTypeNone)
    {
        self.titleType = JHTitleTypeNone;
    }
}

#pragma mark - 设置
/**
 *  设置title样式
 *  设置约束 去除不需要的
 *  @param titleType 主要是右边的按钮
 */
-(void)setTitleType:(JHTitleType)titleType
{
    _titleType = titleType;

    switch (titleType)
    {
        case JHTitleTypeNone:
        {
//            [self.rightBtn removeFromSuperview];
        }
            break;
        case JHTitleTypeRight:
        {
            
            [self.rightBtn makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self).offset(-5);
                make.centerY.equalTo(self.titleLabel);
                make.size.equalTo(CGSizeMake(34, 44));
            }];
        }
            break;
        case JHTitleTypeCustomSet:
        {
            [self.btnBack removeFromSuperview];
            [self.titleLabel removeFromSuperview];
            [self.rightBtn removeFromSuperview];
        }
        default:
            break;
    }
}

-(void)setIsHidebtnBack:(BOOL)isHidebtnBack
{
    _isHidebtnBack = isHidebtnBack;
}
// 更改title名称
-(void)setTitle:(NSString *)title
{
    _title = [title copy];
    self.titleLabel.text = title;
   [ self.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];

}


#pragma mark - 事件

-(void)returnClick:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(jhTitleView:clickBackButton:)])
    {
        [self.delegate jhTitleView:self clickBackButton:sender];
    }
}


@end
